<template>
	<f7-page>
		<f7-card style="margin-top:50px;">
			<f7-card-header style="background-repeat:no-repeat; background-size:100% 100%; height: 90px !important;"
				:style="{'background-image': 'url(' + require('../assets/title.png')+')'}">
			</f7-card-header>
			<f7-card-content style="text-align:center;">
				<p>
					<span style="color:#D3D3D3; font-size:24px; font-weight:bold; margin-right:15px;">用户认证中...</span>
					<f7-preloader color="multi"></f7-preloader>
				</p>
			</f7-card-content>
		</f7-card>	
	</f7-page>
</template>

<script>
import Common from '@/utils/Common.js';
import request from '@/utils/request.js';
import { setUser } from '@/utils/auth.js';
import { raw, sha1 } from '@/utils/weixin';

export default {
	data() {
		return {
			
		}
	},
	methods: {
		
	},
	created() {
		let self = this;
		let code = Common.getQueryVariable(window.location.search, 'code');
		request({
			baseURL: process.env.VUE_APP_BASE_API,
			url: 'api/weixin/jwt',
			params: {code}
		}).then(data => {
			let member = data.element;
			setUser(member);
			var ret = {
				jsapi_ticket: data.element.weixinTicket,
				nonceStr: parseInt(new Date().getTime() / 1000) + '',
				timestamp: Math.random().toString(36).substr(2, 15),
				url: window.location.href
			};
			var string = raw(ret);
			ret.signature = sha1(string);
			
			window.wx.config({
				beta: true,
				debug: false, 
				appId: 'ww8253d1ca9e938eba', 
				timestamp: ret.timestamp, 
				nonceStr: ret.nonceStr,
				signature: ret.signature,
				jsApiList: ['scanQRCode']
			});
			self.$f7router.navigate('/');
		}).catch(error => {
			let dialog = self.$f7.dialog.create({
				text: error.detail.msg,
				title: '认证失败',
				buttons: [{
					text: '加入方法',
					onClick: () => {
						window.location.href = "https://nc.gznu.edu.cn/__local/8/4F/26/997258763438E4833DBB769F368_F7FE43F0_2A3B7.png"
					}
				}, {
					text: '确定'
				}]
			});
			dialog.open();
			console.error(error);
		});
	}
}
</script>

<style scoped>

</style>